package com.chen.blog.controller;


import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.model.Comment;
import com.chen.blog.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Api(tags = "评论管理 CommentController")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @ApiOperation("检索评论")
    @GetMapping("/{id}/{currentPage}/{pageSize}")
    public CommonResult getCommentByBlogId(@PathVariable Integer id, @PathVariable(value = "currentPage") Integer currentPage,
                                           @PathVariable(value = "pageSize") Integer pageSize, @RequestParam Map<String, Object> map) {
        return commentService.getCommentByBlogId(id, currentPage, pageSize, map);
    }

    @ApiOperation("新增评论")
    @PostMapping
    public CommonResult addCommentByBlogId(@RequestBody Comment comment) {
        if (commentService.save(comment)) {
            return CommonResult.success(MessageConstant.ADD_COMMENT_SUCCESS);
        }
        return CommonResult.error(MessageConstant.ADD_COMMENT_FAIL);
    }

    @ApiOperation("点赞评论")
    @PutMapping
    public CommonResult addUp(@RequestBody Comment comment) {
        if (commentService.updateById(comment)) {
            return CommonResult.success(MessageConstant.ADD_UP_SUCCESS);
        }
        return CommonResult.error(MessageConstant.ADD_UP_FAIL);
    }

    @ApiOperation("删除评论")
    @DeleteMapping("/{id}")
    public CommonResult deleteCommentByBlogId(@PathVariable Integer id) {
        if (commentService.removeById(id)) {
            return CommonResult.success(MessageConstant.DELETE_POSITION_SUCCESS);
        }
        return CommonResult.error(MessageConstant.DELETE_POSITION_FAIL);
    }
}
